using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeachingAffairsManagementSystem.WebControl;
using WebGrease.Css.Ast.Selectors;

namespace TeachingAffairsManagementSystem.UserControl.RightContent.Admin
{
    public partial class LessonSelectionManagement : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["l_id"] != null)
                {
                    Session["DSLessonSelectionCommand"] = null;
                    TBSearch.Text = Request.QueryString["l_id"];
                    LBSearchL_ID_Click(null, null);
                }
                else if (Request.QueryString["s_id"] != null)
                {
                    TBSearch.Text = Request.QueryString["s_id"];
                    LBSearchS_ID_Click(null, null);
                }
                else if (Request.QueryString["t_id"] != null)
                {
                    TBSearch.Text = Request.QueryString["t_id"];
                    LBSearchT_ID_Click(null, null);
                }
            }
            else
            {
                if (Session["DSLessonSelectionCommand"] != null)
                {
                    DSLessonSelection.SelectCommand = Session["DSLessonSelectionCommand"].ToString();
                }
            }
        }

        protected void LBSearchAll_Click(object sender, EventArgs e)
        {
            DSLessonSelection.SelectCommand = $"SELECT * FROM view_student_has_lesson";
            Session["DSLessonSelectionCommand"] = DSLessonSelection.SelectCommand;
            TBSearch.Text = null;
            GVLessonSelection.EditIndex = -1;
            GVLessonSelection.DataBind();
        }

        protected void LBSearchL_ID_Click(object sender, EventArgs e)
        {
            try
            {
                int l_id = Convert.ToInt32(TBSearch.Text);
                DSLessonSelection.SelectCommand = $"SELECT * FROM view_student_has_lesson where lesson_l_id = {l_id}";
                Session["DSLessonSelectionCommand"] = DSLessonSelection.SelectCommand;
                GVLessonSelection.EditIndex = -1;
                GVLessonSelection.DataBind();
            }
            catch
            {
                LBSearchAll_Click(null, null);
            }
        }

        protected void LBSearchT_ID_Click(object sender, EventArgs e)
        {
            try
            {
                int t_id = Convert.ToInt32(TBSearch.Text);
                DSLessonSelection.SelectCommand =
                    $"SELECT * FROM view_student_has_lesson where lesson_teacher_t_id = {t_id}";
                Session["DSLessonSelectionCommand"] = DSLessonSelection.SelectCommand;
                GVLessonSelection.EditIndex = -1;
                GVLessonSelection.DataBind();
            }
            catch
            {
                LBSearchAll_Click(null, null);
            }
        }

        protected void LBSearchS_ID_Click(object sender, EventArgs e)
        {
            try
            {
                int s_id = Convert.ToInt32(TBSearch.Text);
                DSLessonSelection.SelectCommand = $"SELECT * FROM view_student_has_lesson where student_s_id = {s_id}";
                Session["DSLessonSelectionCommand"] = DSLessonSelection.SelectCommand;
                GVLessonSelection.EditIndex = -1;
                GVLessonSelection.DataBind();
            }
            catch
            {
                LBSearchAll_Click(null, null);
            }
        }

        protected void GVLessonSelection_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (e.NewValues["score"] == null)
            {
                Alert.Show("请输入合法分数", AlertBox.Level.Danger, true);
                e.Cancel = true;
                return;
            }

            int score = Convert.ToInt32(e.NewValues["score"]);
            if (score < 0 || score > 100)
            {
                Alert.Show("请输入合法分数", AlertBox.Level.Danger, true);
                e.Cancel = true;
                return;
            }
        }

        protected void GVLessonSelection_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            Alert.Show("更新成功", AlertBox.Level.Success, true);
        }

        protected void GVLessonSelection_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            Alert.Show("删除成功", AlertBox.Level.Success, true);
        }
    }
}